<?php
namespace App\Http\Admin\Product\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Http\Admin\Website\Models\Menu;
use App\Eloquent\Ygt\Product;
use App\Eloquent\Ygt\Supplier;
use App\Eloquent\Ygt\ProductSupplier;
use App\Engine\Product as EngineProduct;

class SupplierController extends Controller
{
    protected $modelName = 'ProductSupplier';
    protected $company = false;

    protected $rules = [
        'supplier_id' => 'bail|required|integer',
        'product_id' => 'required|integer',
        'number' => 'integer',
    ];

    public function __construct()
    {
        parent::__construct();
        $this->model = new ProductSupplier();

    }


    protected function _where(){

        $where = [];
        if($productId = request('product_id')){
            $where['product_id'] = $productId;
        }
        if($supplierId = request('supplier_id')){
            $where['supplier_id'] = $supplierId;
        }
        return $where;
    }

    public function set(){
        $productId = request('productId');
        $suppliers = request('suppliers',[]);
        $delSuppliers = request('del_suppliers',[]);

        $ProductSupplierModel = new ProductSupplier();

        if($delSuppliers){
            $ProductSupplierModel->del(array_merge(['product_id'=>$productId],['supplier_id'=>['notIn',$delSuppliers]]));
        }else{
            $ProductSupplierModel->del(['product_id'=>$productId]);
        }


        $createData = [];
        foreach ($suppliers as $val){
            $createData[] = [
                'product_id' => $productId,
                'supplier_id' => $val
            ];
        }
        if($createData){
            $ProductSupplierModel->insertData($createData);
        }

        return $this->ajaxSuccess();

    }

    public function listAll(){

        $productId = request('product_id');
        $adminUser = request()->user('admin')->toArray();
        $companyId = $adminUser['company_id'];

        $proModel = new Product();
        $info = $proModel->getOneData(['id'=>$productId]);

        $productSupplierList = $info->supplier->pluck('id');

        $supplierModel = new Supplier();
        $supplierList = $supplierModel->getData(['company_id'=>$companyId]);

        $supplierList->transform(function($item) use($productSupplierList){
            $item->isCheck = in_array($item->id,$productSupplierList->toArray()) ? 1 : 0;
            return $item;
        });

        return $this->view('list',compact('supplierList','productId'));

    }

    protected function _fields(){
        return 'ygt_product.product_name,ygt_supplier.supplier_name,ygt_product_supplier.*';
    }

    protected function _orderby(){
        return ['ygt_product_supplier.id','desc'];
    }

    protected function _join(){
        return [
            ['table'=>'ygt_product', 'field_l'=>'ygt_product.id', 'field_c'=>'=', 'field_r'=>'ygt_product_supplier.product_id'],
            ['table'=>'ygt_supplier', 'field_l'=>'ygt_supplier.id', 'field_c'=>'=', 'field_r'=>'ygt_product_supplier.supplier_id'],
        ];
    }

    protected function _vdataAdd(){
        $productInfo = Product::getOneData(['id'=>request('product_id')]);
        return ['productId'=>$productInfo['id'],'productName'=>$productInfo['product_name']];
    }
    protected function _vdata(){
        return [
            'productId'=>request('product_id')
        ];
    }

    public function customSave(){
        $pk = $this->model->getPrimaryKey();
        if (request()->isMethod('post')) {
            $param = request()->all();

            $result = $this->validateRequestParameters($param, $this->rules, false);

            if ($result) {
                return $this->ajaxFail($result);
            }

            $result = ProductSupplier::updateOrCreate(['supplier_id'=>$param['supplier_id'],'product_id'=>$param['product_id']],['number'=>$param['number']]);

            if ($result) {
                return $this->ajaxSuccess();
            } else {
                return $this->ajaxFail();
            }
        }
        if($id = request($pk)){
            $info = $this->model->getOneData([$pk=>$id]);
            $info = $this->_honedata($info);
            $vdata = $this->_vdataAdd();
            $vdata['info'] = $info;
            return $this->view('edit',$vdata);
        }else{
            $vdata = $this->_vdataAdd();
            return $this->view('add',$vdata);
        }

    }

    /*    protected function _vdata(){
            $fieldsType = EngineProduct::fieldsType();
            foreach ($fieldsType as $k=>$v){
                $fieldsType[$k] = $v['title'];
            }
            $data = ['fieldsType'=>json_encode($fieldsType)];

            return $data;
        }

        protected function _hdata($result){
            $result->transform(function($item){
                $comumnName = EngineProduct::getFieldColumn($item->field_type);
                $item->field_value = $item->$comumnName;
            });
            return $result;
        }*/

}